﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post.Domain;
using Common.Logging;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;

public partial class success : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private PostFactory m_PostFactory;
    private IPostService m_PostService;

    //成功見證NodeId=13
    private int m_NodeId = 13;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();

        if (!IsPostBack)
        {
            //清單
            Repeater_Bind();
        }
    }

    private void Repeater_Bind()
    {
        //搜尋條件
        DateTime? startDate = DateTime.Today;
        string sortField = "ShowDate";
        bool sortDesc = true;

        //分頁
        AspNetPager1.RecordCount = m_PostService.CountPostListByNodeIdWithCloseDate(m_NodeId, true, startDate);
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;

        Repeater1.DataSource = m_PostService.GetPostListByNodeIdWithCloseDate(m_NodeId, true, startDate, pageIndex, pageSize, sortField, sortDesc);
        Repeater1.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Repeater_Bind();
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            PostVO postVO = (PostVO)e.Item.DataItem;

            //圖片
            if (!string.IsNullOrEmpty(postVO.PicFileName))
            {
                string img = string.Format("<img src='upload/{0}' width='163' height='111' border='0' />"
                    , postVO.PicFileName);
                UIHelper.SetLiteralText(ctrl, "ltlImg", img);
            }

            //日期
            string date = postVO.ShowDate.Value.ToString("yyyy.MM.dd");
            UIHelper.SetLiteralText(ctrl, "ltlDate"
                , string.Format("<div class='photo-day'>{0}</div>", date));

            //標題
            int titleLength = 25;
            if (!string.IsNullOrEmpty(postVO.Title))
            {
                string title = postVO.Title;

                if (title.Length > titleLength)
                {
                    title = title.Substring(0, titleLength) + "...";
                }

                UIHelper.SetLiteralText(ctrl, "ltlTitle"
                    , string.Format("<div class='news-title2'>{0}</div>", title));
            }
        }
    }
}